package com.hrms.controller;


import com.hrms.pojo.EmpInformation;
import com.hrms.pojo.PersonnelTransfer;
import com.hrms.pojo.UserRoleRef;
import com.hrms.service.PersonnelTransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * <p>
 * 人事调动表 前端控制器
 * </p>
 *
 * @author LK
 * @since 2021-10-19
 */
@RestController
@RequestMapping("/personnelTransfer")
public class PersonnelTransferController {
    @Autowired(required = false)
    PersonnelTransferService personnelTransferService;

    //返回人事调动数据
    @RequestMapping("/personnelTransferList")
    public Map<String,Object> listPage(Model model, PersonnelTransfer p){
        return personnelTransferService.select(p);
    }

    //添加人事调动数据
    @RequestMapping("/addPersonnelTransfer")
    public Map<String,Object> addPersonnelTransfer(Model model, PersonnelTransfer p){
        return personnelTransferService.add(p);
    }

    //修改人事调动数据
    @RequestMapping("/updatePersonnelTransfer")
    public Map<String,Object> updatePersonnelTransfer(Model model, PersonnelTransfer p){
        String newJob = p.getNewJob();
        int i = personnelTransferService.selectRoleIdByRoleName(newJob);
        UserRoleRef userRoleRef = personnelTransferService.selectUserRoleRef(p.getStaffId());
        userRoleRef.setRoleId(i);
        personnelTransferService.updateUserRole(userRoleRef);

        EmpInformation e = new EmpInformation();
        e.setStaffId(p.getStaffId());
        e.setStaffDepartment(p.getNewDepartment());
        e.setStaffPost(p.getNewJob());

        PersonnelTransfer oldP = personnelTransferService.selectOne(p.getId());
        personnelTransferService.upSysDeptNumber(oldP.getNewDepartment(),p.getNewDepartment());

        personnelTransferService.updateEmpInformationByStaffId(e);
        return personnelTransferService.update(p);
    }

    //删除人事调动数据
    @RequestMapping("/deletePersonnelTransfer")
    public Map<String,Object> deletePersonnelTransfer(Model model, PersonnelTransfer p){
        return personnelTransferService.delete(p);
    }

    //返回角色列表
    @RequestMapping("/roleList")
    public Map<String,Object> getRoleList(Model model, String p){
        return personnelTransferService.getRoleListByDept(p);
    }

    //返回员工列表
    @RequestMapping("/empInformationList")
    public Map<String,Object> empInformationList(Model model, EmpInformation e){

        return personnelTransferService.getEmpInformation(e);
    }

    //修改员工列表
    @RequestMapping("/updateEmpInformationList")
    public Map<String,Object> updateEmpInformationList(Model model, EmpInformation e){
        EmpInformation oldE = personnelTransferService.getEmpInformationOne(e);
        int i = personnelTransferService.selectRoleIdByRoleName(e.getStaffPost());
        UserRoleRef userRoleRef = personnelTransferService.selectUserRoleRef(e.getStaffId());
        userRoleRef.setRoleId(i);
        personnelTransferService.updateUserRole(userRoleRef);

        //修改部门人数
        personnelTransferService.upSysDeptNumber(oldE.getStaffDepartment(),e.getStaffDepartment());

        PersonnelTransfer p = new PersonnelTransfer();
        p.setStaffId(e.getStaffId());
        p.setStaffName(e.getStaffName());
        p.setOldDepartment(oldE.getStaffDepartment());
        p.setOldJob(oldE.getStaffPost());
        p.setNewDepartment(e.getStaffDepartment());
        p.setNewJob(e.getStaffPost());
        p.setChangeWhy(e.getChangeWhy());
        personnelTransferService.add(p);
        return personnelTransferService.updateEmpInformation(e);
    }
}

